﻿Partial Public Class GamePage
    Inherits UserControl

    Public Sub New(ByVal GameName As String)
        InitializeComponent()
        TitleLabel.Text = GameName
        Try
            QuestionImage.Source = New Imaging.BitmapImage(New Uri("/Display;component/Images/Icon.jpg"))
        Catch
        End Try
    End Sub

    Sub UpdateScores(ByVal Scores As Dictionary(Of String, Long))
        ScoreboardNames.Items.Clear()
        ScoreboardScores.Items.Clear()
        For Each Score As KeyValuePair(Of String, Long) In Scores
            ScoreboardNames.Items.Add(Score.Key)
            ScoreboardScores.Items.Add(Score.Value)
        Next
    End Sub

    Public Sub GetQuestionCompleted()
        Dim FadeOutAnimation As Storyboard = DirectCast(Me.Resources("FadeOut"), Storyboard)
        FadeOutAnimation.Begin()
    End Sub

    Public Sub StartQuestion()
        Try
            If Not CurrentQuestion.Image Is Nothing Then
                'Dim MS As New IO.MemoryStream(Convert.FromBase64String(CurrentQuestion.Image))
                Dim bitmap As New Imaging.BitmapImage()
                bitmap.CreateOptions = Imaging.BitmapCreateOptions.IgnoreImageCache
                bitmap.UriSource = New Uri(Host & "WebResources/" & GameName.Replace(" ", "%20") & "Image.img")
                'QuestionImage.Source = Nothing
                'bitmap.SetSource(MS)
                QuestionImage.Source = bitmap
            Else
                Try
                    QuestionImage.Source = New Imaging.BitmapImage(New Uri("/Display;component/Images/Icon.jpg"))
                Catch
                End Try
            End If
        Catch ex As Exception
        End Try

        QuestionNoLabel.Text = "Question Number: " & CurrentQuestion.QuestionID
        QuestionTextLabel.Text = CurrentQuestion.QuestionText

        If CurrentQuestion.IsMultichoice = True Then
            ALabel.Text = CurrentQuestion.A
            BLabel.Text = CurrentQuestion.B
            CLabel.Text = CurrentQuestion.C
            DLabel.Text = CurrentQuestion.D
        Else
            ALabel.Text = ""
            BLabel.Text = ""
            CLabel.Text = ""
            DLabel.Text = ""
        End If

        Dim FadeInAnimation As Storyboard = DirectCast(Me.Resources("FadeIn"), Storyboard)
        FadeInAnimation.Begin()
    End Sub

    Public Sub EndQuestion(ByVal CorrectAnswer As String)
        Select Case CorrectAnswer
            Case "A"
                Dim Animation As Storyboard = DirectCast(Me.Resources("ACorrect"), Storyboard)
                Animation.Begin()
            Case "B"
                Dim Animation As Storyboard = DirectCast(Me.Resources("BCorrect"), Storyboard)
                Animation.Begin()
            Case "C"
                Dim Animation As Storyboard = DirectCast(Me.Resources("CCorrect"), Storyboard)
                Animation.Begin()
            Case "D"
                Dim Animation As Storyboard = DirectCast(Me.Resources("DCorrect"), Storyboard)
                Animation.Begin()
            Case Else
                InputCorrectAnswer.Text = "The Correct answer was:" & vbCr & CorrectAnswer
                Dim Animation As Storyboard = DirectCast(Me.Resources("InputCorrect"), Storyboard)
                Animation.begin()
        End Select
    End Sub

    Private Sub ReturnButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles ReturnButton.Click
        DS.MainPageAsync(DisplayID)
    End Sub

    Public Sub EndGame()
        QuestionNoLabel.Text = "Game Closed"
        QuestionTextLabel.Text = ""

        ALabel.Text = ""
        BLabel.Text = ""
        CLabel.Text = ""
        DLabel.Text = ""

        Dim FadeInAnimation As Storyboard = DirectCast(Me.Resources("FadeIn"), Storyboard)
        FadeInAnimation.Begin()
    End Sub
End Class